package com.zxp.springcloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.zxp.springcloud.service.PaymentHystrixService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @description:
 * @author: 张新朋
 * @time: 2020/5/6 11:21
 */
@RestController
@Slf4j
@DefaultProperties(defaultFallback = "global_fallBack")
public class PaymentController {
    @Resource
    private PaymentHystrixService paymentHystrixService;

    @Value("${server.port}")
    private String port;

    @GetMapping("/paymemt/hystrix/ok/{id}")
    public String get_OK(@PathVariable("id") Integer id){
        String ok = paymentHystrixService.get_OK(id);
        return ok+port;
    }
    @GetMapping("/paymemt/hystrix/error/{id}")
/*    @HystrixCommand(fallbackMethod="get_ErrorHandler",commandProperties = {
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="3000")
    })*/
    @HystrixCommand()
    public String get_ERROR(@PathVariable("id") Integer id){
        String error = paymentHystrixService.get_ERROR(id);
        return error+port;
    }

    public String global_fallBack(){
        return "全局统一配置fallback,系统繁忙。。。";
    }
    //====服务熔断
    @GetMapping("/payment/circuit/{id}")
    public String paymentCircuitBreaker(@PathVariable("id") Integer id)
    {
        String result = paymentHystrixService.paymentCircuitBreaker(id);
        log.info("****result: "+result);
        return result;
    }
}